package com.example.mybaseapp.widget.circle_seekbar

import android.graphics.PointF
import kotlin.math.atan2
import kotlin.math.pow
import kotlin.math.sqrt

/**
 * 事件控制
 * 移动、放手、是否可以继续移动
 * 放手后的校准（这个时机放到了绘制的帮助类中，因为要用到步长）
 * 当前选中的数据
 */
class EventController(val drawaUIUtil: DrawaUIUtil) {
    var selectListener:SelectListener?=null

    /**
     * 点击坐标是否在指针上
     */
    fun isTouchCursor(touchPoint:PointF,cursorPointF: PointF):Boolean{
        val distance = sqrt(((touchPoint.x - cursorPointF.x).pow(2) + (touchPoint.y - cursorPointF.y).pow(2)).toDouble())
        return distance <= drawaUIUtil.cursorRadius*1.5
    }

    /**
     * 当前移动的角度多大
     * @param touchPoint 点击的坐标
     * @param movePoint 当前移动的坐标
     * 往左滑是负值
     */
    fun moveDegree(touchPoint: PointF,movePoint:PointF):Float{

        val dy = movePoint.y - drawaUIUtil.circleCenter.y
        val dx = movePoint.x - drawaUIUtil.circleCenter.x


        val aDyDx = atan2(dy.toDouble(), dx.toDouble())
        val a2 = atan2(
            (touchPoint.y - drawaUIUtil.circleCenter.y).toDouble(),
            (touchPoint.x - drawaUIUtil.circleCenter.x).toDouble()
        )

        var degree = Math.toDegrees(aDyDx- a2).toFloat()

        if (degree > 180) {
            degree -= 360
        } else if (degree < -180) {
            degree += 360
        }
        return degree
    }

    /**
     * 移动指针
     */
    fun moveCursor(moveDegree:Float){
        drawaUIUtil.moveCursorDegree = moveDegree
    }

    /**
     *
     * 是否可以移动
     */
    fun moveAdjust(moveDegree: Float, pointDatas: MutableList<PointsData>):Float{
        if(pointDatas.isEmpty()){
            return 0F
        }
//        获取总的旋转度数[ 向左滑是负值，左滑的最大限制是右边的最大值是正直，所以要再负值一次；
//        右滑是正直，右滑最右边限制是最小值是负值，一样再负值一次
        val tmp = -(drawaUIUtil.currentScaleDegree + moveDegree)
        println("当前旋转的角度: canMove $tmp ${pointDatas[0].degree+90}  ${pointDatas[pointDatas.size-1].degree+90}")
        if(tmp<=pointDatas[0].degree+90){
            return - (pointDatas[0].degree+90F) - drawaUIUtil.currentScaleDegree
        }
        if(tmp>=pointDatas[pointDatas.size-1].degree+90){
            return - (pointDatas[pointDatas.size-1].degree+90F) - drawaUIUtil.currentScaleDegree
        }
        return moveDegree
    }

    /**移动刻度*/
    fun moveScale(moveDegree:Float){
        println("当前旋转的角度: moveScale $moveDegree")
        drawaUIUtil.moveScaleDegree = moveDegree
    }

    /**
     * 放手保存当前移动的角度
     */
    fun upScale(){
        drawaUIUtil.currentScaleDegree += drawaUIUtil.moveScaleDegree
        drawaUIUtil.moveScaleDegree = 0F
        drawaUIUtil.upScale()
        println("当前旋转的角度: currentScaleDegree=${drawaUIUtil.currentScaleDegree}")
    }

    /**
     * 当前选中的角度
     */
    fun currentSelect(pointDatas: MutableList<PointsData>){
        val fl = -drawaUIUtil.currentScaleDegree - 90F
        for(d in pointDatas){
            if(fl==d.degree){
                selectListener?.currentSelect(d)
                return
            }
        }
    }
}